package com.grt.admin.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grt.admin.AdminConstants;
import com.grt.admin.domain.Case;
import com.grt.admin.domain.dto.CaseDTO;
import com.grt.admin.service.impl.CaseServiceImpl;
import com.grt.common.BaseVo;
import com.grt.common.config.SystemConfig;
import com.grt.common.convert.impl.CaseDOConvertDTO;
import com.grt.common.convert.impl.CaseDTOConvertDO;
import com.grt.common.exception.WebException;
import com.grt.common.util.FileUtil;
import com.grt.common.util.UserUtil;
import com.grt.file.FileConstants;
import com.grt.file.domain.Appendix;
import com.grt.file.service.AppendixService;
import com.grt.user.domain.User;

@Controller
@RequestMapping(value = "/case")
public class CaseController {

	@Autowired
	private CaseServiceImpl caseService;

	@Autowired
	private AppendixService appendixService;

	@Autowired
	private SystemConfig systemConfig;

	@Autowired
	private CaseDTOConvertDO caseDTOConvertDO;

	@Autowired
	private CaseDOConvertDTO caseDOConvertDTO;

	private static final Logger logger = LoggerFactory.getLogger(CaseController.class);

	/**
	 * 保存信息
	 * 
	 * @param oCaseDTO
	 *            在C层使用DTO对象来校验属性
	 * @return
	 * @throws WebException
	 */
	@ResponseBody
	@PostMapping("/saveCase")
	public BaseVo<Case> saveCase(@Valid CaseDTO oCaseDTO) throws WebException {

		BaseVo<Case> vo = new BaseVo<Case>();

		Case oCase = caseDTOConvertDO.convert(oCaseDTO);

		// 获取当前登录用户
		User curUser = UserUtil.getLoginUser();
		if (curUser == null) {
			throw new WebException("无法获取当前登录用户");
		}

		// 处理附件图片
		String file = oCaseDTO.getFile();
		Appendix oAppendix = appendixService.getAppendix(file);
		oAppendix.setAppFile(file);
		oAppendix.setAppDesc(file);
		oAppendix.setAppFileType(FileConstants.FILETYPE_PIC);
		oAppendix.setCreateTime(new Date());
		oAppendix.setCreateUserId(curUser.getId());
		oAppendix.setCreateUsername(curUser.getUsername());
		long appendixid = appendixService.saveAppendix(oAppendix);

		oCase.setCaseimg(appendixid);

		oCase = caseService.saveCase(oCase);

		vo.setDatas(oCase);

		return vo;
	}

	@RequestMapping("/caselist")
	public String caselist(ModelMap modelMap, Integer pageindex, Integer pagesize) throws WebException {
		pageindex = pageindex == null ? AdminConstants.DEF_PAGEINDEX : pageindex;
		pagesize = pagesize == null ? AdminConstants.DEF_PAGESIZE : pagesize;
		System.out.println(pageindex + "|" + pagesize);
		Page<Case> page = caseService.getCaseList(pageindex, pagesize);
		List<Case> list = page.getContent();
		long totalCount = page.getTotalElements();
		int totalPage = page.getTotalPages();

		modelMap.addAttribute("datas", list);
		modelMap.addAttribute("totalCount", totalCount);
		modelMap.addAttribute("totalPage", totalPage);
		modelMap.addAttribute("pagesize", pagesize);
		modelMap.addAttribute("pageindex", pageindex);
		modelMap.addAttribute("typeList", AdminConstants.caseTypeMap);

		return "admin/caselist";
	}
	

	/**
	 * 获取案列列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getCaseList")
	public BaseVo<List<Case>> getCaseList(Integer pageindex, Integer pagesize) {
		pageindex = pageindex == null ? AdminConstants.DEF_PAGEINDEX : pageindex;
		pagesize = pagesize == null ? AdminConstants.DEF_PAGESIZE : pagesize;
		BaseVo<List<Case>> vo = new BaseVo<List<Case>>();
		Page<Case> page = caseService.getCaseList(pageindex, pagesize);
		List<Case> list = page.getContent();
		long totalCount = page.getTotalElements();
		int totalPage = page.getTotalPages();

		vo.setDatas(list);
		vo.setCurPage(pageindex);
		vo.setTotalCount(totalCount);
		vo.setTotalPage(totalPage);
		return vo;
	}

	@GetMapping("/addcase")
	public String addcase() throws WebException {
		return "admin/addcase";
	}
	
	/**
	 * 获取案列信息
	 * 
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getCaseInfo")
	public BaseVo<Case> getCaseInfo(long id) {
		return null;
	}

	/**
	 * 获取案列信息
	 * 
	 * @return
	 */
	@GetMapping("/updateCase")
	public String updateCase(ModelMap modelMap, long id) {
		Case oCase = caseService.getCaseById(id);
		// 处理案例中的图片

		Long img = oCase.getCaseimg();
		String sCaseimg = systemConfig.getServerHost() + File.separator + "admin/images/default_case.jpg";
		if (null != img) {
			Appendix oAppendix = appendixService.getAppendix(img);
			sCaseimg = systemConfig.getServerHost() + File.separator
					+ FileUtil.getFilePathByFileName(oAppendix.getAppFile());
		}

		CaseDTO dto = caseDOConvertDTO.convert(oCase);
		dto.setFile(sCaseimg);

		modelMap.addAttribute("data", dto);
		return "admin/editcase";
	}

	/**
	 * 删除案列
	 * 
	 * @return
	 */
	@ResponseBody
	@GetMapping("/deleteCase")
	public BaseVo<Case> deleteCase(long id) {
		BaseVo<Case> vo = new BaseVo<Case>();
		Case oCase = caseService.getCaseById(id);
		if (null == oCase) {
			logger.info("要删除的对象不存在，id=【" + id + "】");
			vo.setFailed("要删除的案列不存在！");
		}
		caseService.deleteCase(oCase);
		vo.setSuccessed("删除案列成功！");
		return vo;
	}

}
